#include "Cpersuit.h"

void
cps_stopwatch_start(Cps_Stopwatch *watch)
{
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &watch->start);
}

void 
cps_stopwatch_stop(Cps_Stopwatch *watch)
{
	struct timespec now;
	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now);
	watch->time.tv_sec += now.tv_sec - watch->start.tv_sec;
	watch->time.tv_nsec += now.tv_nsec - watch->start.tv_nsec;

	/* normalize */
	if (watch->time.tv_nsec >= 1E9)
	{
		watch->time.tv_nsec -= 1E9;
		watch->time.tv_sec++;
	}
	if (watch->time.tv_nsec < 0)
	{
		watch->time.tv_nsec += 1E9;
		watch->time.tv_sec--;
	}
}

